// 初始化转换
export function initTransferData(modelInfo, targetColumns) {
  let time = new Date().getTime();
  let random = Math.floor(Math.random() * 10000);
  const initJsonDetail = () => {
    let outputTableCell = {
      data: {
        ...JSON.DeepCopy({
          ...TableOutput,
          id: `output_table_${random}_${time}`,
          dbId: modelInfo.datasourceId,
          datasourceName:  modelInfo.datasourceName,
          schema: modelInfo.schemaName,
          schemaId: modelInfo.schemaName,
          tableCode: modelInfo.tableCode,
          tableId: modelInfo.tableCode,
          tableName: modelInfo.tableName,
          columns: targetColumns,
          mapping: [],
          sourceNodeType: "",
        })
      },
      label: modelInfo.tableCode,
      shape: 'custom-input',
      size: { width: 160, height: 36 },
      view: 'vue-shape-view',
      zIndex: 2,
      ports: {},
      position: {},
    };

    let newJob = {
      //节点内容
      steps: [inputTableCell, joinTableCell, sqlTableCell, outputTableCell],
      //连线关系
      hops: [],
    }
    return JSON.stringify(newJob);
  };
  return {
    ...transferInfo,
    jobName: "",
    mappingType: "join",
    jsonDetail: initJsonDetail()
  }
}

export const transferInfo = {
  id: undefined,
  orgId: undefined,
  jobName: undefined,
  parentId: undefined,
  jsonDetail: "{}",
  jsonDetailBak: "",
  jobDesc: undefined,
  jobType: undefined,
  jobClass: undefined,
  jobGroupId: undefined,
  jobExtra: "{}",
  scheduleTaskId: undefined,
  origJobId: undefined,
  jobVersion: undefined,
  masterId: undefined,
  replicaId: undefined
}

export const TableInput = {
  id: "TableInput",
  name: '库表输入',
  code: "",
  type: "TableInput", //TableOutput:表库输出 TableJoin表合并关联
  sourceType: 1, //数据来源 0:元数据平台   1:实时采集
  clusterId: undefined, //默认集群ID
  dbId: undefined, //链接数据库ID
  dbName: undefined, //链接数据库名称
  dbType: undefined, //链接数据库类型
  schema: undefined, //目标schema,映射schemaCode
  schemaId: undefined,
  tableId: undefined, //选择的数据表ID
  tableName: undefined, //选择的数据表名称
  tableCode: undefined, //选择的数据表名称
  asName: undefined, //选择的数据表名称
  tableColumns: [], //数据源字段信息
  columns: [], //输出字段信息
  lookUp: false, //是否维表
  eventTimeColumn: undefined, //事件执行字段，必须时间格式，且必须出现在columns中
  description: undefined,
  whereString: undefined,
  dictColumn: undefined,
  activSource: undefined, //激活的数据源，空是是根节点的
  sourceList: [], //节点的多数据源{id[时间戳生成] sourceType dbId dbName dbType schema schemaId }
  nestedSql: undefined,//去重脚本
  isMain: false,
}

export const TableOutput = {
  id: 'TableOutput',
  name: '库表输出',
  code: undefined,
  type: 'TableOutput',
  dbId: undefined, //链接数据库ID
  dbName: undefined, //链接数据库名称
  clusterId: undefined, //默认集群ID
  dbType: undefined, //链接数据库类型
  tableId: undefined, //选择的数据表ID
  tableName: undefined, //选择的数据表名称
  tableCode: undefined, //选择的数据表名称
  asName: 'temp', //选择的数据表名称
  tableColumns: [], //数据源字段信息
  sourceType: 1,
  operateType: 'merge', //操作类型
  columns: [], //输出字段信息
  schema: undefined, //目标schema,映射schemaCode
  schemaId: undefined,
  sourceNodeType: undefined, //来源节点类型
  kafkaTable: undefined,
  lookUp: false, //是否维表
  eventTimeColumn: undefined, //事件执行字段，必须时间格式，且必须出现在columns中
  mapping: [], //映射字段
  inputTable: {
    //输入表格
    tableId: undefined, //选择的数据表ID
    tableName: undefined, //选择的数据表名称
    tableCode: undefined, //选择的数据表名称
    columns: [], //输出字段
  },
  whereString: undefined,
  description: undefined,
  activeSource: undefined, //激活的数据源，空是是根节点的
  sourceList: [], //节点的多数据源{id[时间戳生成] sourceType dbId dbName dbType schema schemaId }
};

export const SqlInput = {
  id: 'SqlInput',
  name: '脚本输入',
  code: undefined,
  type: 'SqlInput',
  sourceType: 1, //数据来源 0:元数据平台   1:实时采集
  clusterId: undefined, //默认集群ID
  dbId: undefined, //链接数据库ID
  dbName: undefined, //链接数据库名称
  dbType: undefined, //链接数据库类型
  modelType: undefined, //模式类型
  schema: undefined, //目标schema,映射schemaCode
  schemaId: undefined,
  tableId: 'sqlTable', //选择的数据表ID
  tableName: 'sqlTable', //选择的数据表名称
  tableCode: 'sqlTable', //选择的数据表名称
  asName: 'sqlTable', //选择的数据表名称
  columns: [], //输出字段信息
  lookUp: true, //是否维表
  script: undefined, //执行脚本
  eventTimeColumn: undefined, //事件执行字段，必须时间格式，且必须出现在columns中
  whereString: undefined,
  description: undefined,
  places: [], //占位符
}

export const TableJoin = {
  id: 'TableJoin',
  name: '库表合并',
  code: undefined,
  type: 'TableJoin',
  tableId: 'temp', //选择的数据表ID
  tableName: 'temp', //选择的数据表名称
  tableCode: 'temp', //选择的数据表名称
  asName: 'temp', //选择的数据表名称
  columns: [], // formerName:med_insti_no,//物理字段名  columnName: "eeed", //别名
  tables: [], //关联的表信息  { tableid, tableName,tableCode,  asName: "a", columns, isMain }, //isMain是否主表
  joins: [], //绑定的关系信息
  lookUp: false,
  description: undefined,
  nestedSql: undefined, //去重脚本
}

export const inputTableCell = {
  data: {
    ...JSON.DeepCopy(TableInput),
    id: `input_table_${Math.floor(Math.random() * 10000)}_${new Date().getTime()}`
  },
  label: $t('库表输入'),
  shape: 'custom-input',
  size: { width: 160, height: 36 },
  view: 'vue-shape-view',
  zIndex: 1,
  ports: {},
  position: {},
}

export const joinTableCell = {
  data: {
    ...JSON.DeepCopy(TableJoin),
    id: `join_table_${Math.floor(Math.random() * 10000)}_${new Date().getTime()}`,
  },
  label: $t('库表合并'),
  shape: 'custom-input',
  size: { width: 160, height: 36 },
  view: 'vue-shape-view',
  zIndex: 1,
  ports: {},
  position: {},
}

export const sqlTableCell = {
  data: {
    ...JSON.DeepCopy(SqlInput),
    id: `sql_table_${Math.floor(Math.random() * 10000)}_${new Date().getTime()}`,
  },
  label: $t('脚本输入'),
  shape: 'custom-input',
  size: { width: 160, height: 36 },
  view: 'vue-shape-view',
  zIndex: 1,
  ports: {},
  position: {},
}

export const startNodeJson = {
  data: {
    id: `start_${Math.floor(Math.random() * 10000)}_${new Date().getTime()}`,
    name: '开始',
    type: 'Start',
    sourceType: 1
  },
  zIndex: 1,
  position: {
    x: 50,
    y: 150
  },
  size: {
    width: 66,
    height: 36
  },
  visible: true,
  shape: 'custom-rect',
  ports: {},
}

export const endNodeJson = {
  data: {
    id: `start_${Math.floor(Math.random() * 10000)}_${new Date().getTime()}`,
    name: '结束',
    type: 'End',
    sourceType: 1
  },
  zIndex: 1,
  position: {
    x: 550,
    y: 150
  },
  size: {
    width: 66,
    height: 36
  },
  visible: true,
  shape: 'custom-rect',
  ports: {},
}

// 连线特性
export const lineoption = {
  attrs: {
    line: {
      stroke: '#A2B1C3',
      strokeWidth: 2,
      targetMarker: { name: 'block', width: 12, height: 8 },
    },
  },
  zIndex: 0,
};

// 调度字段
export const scheduleInfo = {
  jobInfoId: undefined,
  jobDesc: undefined,
  scheduleType: "CRON",
  alarmEmail: undefined,
  triggerStatus: 0,
  scheduleCronConf: {},
  sConf: "每月每日第1时第0分",
  jobGroup: undefined,
  glueType: 'BEAN',
  executorHandler: undefined,
  executorParam: undefined,
  clusterId: undefined,
  jobType: undefined,
  newJobId: undefined,
  otherTaskId: "",
  kafkaInuse: false,
  operatingMode: "BATCH",
  executorRouteStrategy: "FIRST",
  childJobid: undefined,
  misfireStrategy: "DO_NOTHING",
  executorBlockStrategy: "DISCARD_LATER",
  executorTimeout: 0,
  executorFailRetryCount: 0,
}

export const scheduleCronConf = {
  second: '0',
  minute: '0',
  hour: '1',
  day: '*',
  month: '*',
  week: '?',
  year: '',
}

// 调度类型
export const scheduleList = [
  {
    label: $t('周期'),
    value: 'CRON',
  },
  {
    label: $t('手动'),
    value: 'MANUAL',
  },
  {
    label: $t('实时'),
    value: 'REALTIME',
  },
];

//Flink作业模式
export const operModeList = [
  {
    label: 'BATCH',
    value: 'BATCH',
  },
  {
    label: 'STREAMING',
    value: 'STREAMING',
  },
];

// 路由策略
export const routeList = [
  {
    label: $t('第一个'),
    value: 'FIRST',
  },
  {
    label: $t('最后一个'),
    value: 'LAST',
  },
  {
    label: $t('轮询'),
    value: 'ROUND',
  },
  {
    label: $t('随机'),
    value: 'RANDOM',
  },
  {
    label: $t('一致性HASH'),
    value: 'CONSISTENT_HASH',
  },
  {
    label: $t('最不经常使用'),
    value: 'LEAST_FREQUENTLY_USED',
  },
  {
    label: $t('最近最久未使用'),
    value: 'LEAST_RECENTLY_USED',
  },
  {
    label: $t('故障转移'),
    value: 'FAILOVER',
  },
  {
    label: $t('忙碌转移'),
    value: 'BUSYOVER',
  },
  {
    label: $t('分片广播'),
    value: 'SHARDING_BROADCAST',
  },
];

// 调度过期策略
export const misfireList = [
  {
    label: $t('忽略'),
    value: 'DO_NOTHING',
  },
  {
    label: $t('立即执行一次'),
    value: 'FIRE_ONCE_NOW',
  },
];

// 阻塞处理策略
export const executorBlockList = [
  {
    label: $t('丢弃后续调度'),
    value: 'DISCARD_LATER',
  },
  {
    label: $t('单机串行'),
    value: 'SERIAL_EXECUTION',
  },
  {
    label: $t('覆盖之前调度'),
    value: 'COVER_EARLY',
  },
];